package com.guomei.service.curse.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.curse.Course;
import com.guomei.bean.curse.CourseClassAssociation;
import com.guomei.bean.curse.vo.CourseClassAssociationVo;
import com.guomei.mapper.curse.CourseClassAssociationMapper;
import com.guomei.mapper.curse.CourseMapper;
import com.guomei.service.curse.CourseClassAssociationService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程班关联课程服务实现类
 */
@Service
@Slf4j
@Transactional
public class CourseClassAssociationServiceImpl implements CourseClassAssociationService {
    
    @Resource
    private CourseClassAssociationMapper courseClassAssociationMapper;


    @Resource
    private CourseMapper courseMapper;

    @Override
    public CourseClassAssociation getById(Long id) {
        if (id == null) {
            return null;
        }
        return courseClassAssociationMapper.selectById(id);
    }

    @Override
    public List<CourseClassAssociation> getAll() {
        return courseClassAssociationMapper.selectAll();
    }

    @Override
    public Result<?> getByCondition(CourseClassAssociation courseClassAssociation) {
        PageHelper.startPage(courseClassAssociation.getPageNum(), courseClassAssociation.getPageSize());
        // 使用联表查询返回CourseClassAssociationVo对象
        List<CourseClassAssociationVo> associations = courseClassAssociationMapper.selectByConditionWithCourses(courseClassAssociation);
        // 封装分页结果到PageData
        PageData<CourseClassAssociationVo> pageData = new PageData<>();
        PageInfo<CourseClassAssociationVo> pageInfo = new PageInfo<>(associations);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(courseClassAssociation.getPageNum());
        pageData.setPageSize(courseClassAssociation.getPageSize());
        return Result.success(pageData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> save(CourseClassAssociation courseClassAssociation) {
        if (courseClassAssociation.getMainCourseId() == null) {
            return Result.fail("主课程ID不能为空");
        }
        Course course = courseMapper.selectById(courseClassAssociation.getMainCourseId());
        List<Course> courses = courseMapper.selectByIds(courseClassAssociation.getDeputyCourseIds());
        //检查主课程是否是3/4的类型
        if (course != null && (course.getCourseType() != 3 && course.getCourseType() != 4)){
            return Result.fail("主课程类型错误或不存在");
        }
        for (Course cours : courses) {
            if (cours != null && (cours.getCourseType() == 3 || cours.getCourseType() == 4)){
                return Result.fail("批量下属课程类型错误或不存在");
            }
        }
        // 删除该主课程的所有现有关联
        courseClassAssociationMapper.deleteByMainCourseId(courseClassAssociation.getMainCourseId());
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        
        // 循环处理每个下属课程ID，为每个下属课程创建关联记录
        for (Long deputyCourseId : courseClassAssociation.getDeputyCourseIds()) {
            if (deputyCourseId != null) {
                CourseClassAssociation courseClassAssociation1 = new CourseClassAssociation();
                courseClassAssociation1.setMainCourseId(courseClassAssociation.getMainCourseId());
                courseClassAssociation1.setDeputyCourseId(deputyCourseId);
                courseClassAssociation1.setCreatedTime(now);
                courseClassAssociation1.setUpdatedTime(now);
                courseClassAssociationMapper.insert(courseClassAssociation1);
            }
        }

        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<CourseClassAssociation> courseClassAssociations) {
        if (courseClassAssociations == null || courseClassAssociations.isEmpty()) {
            return false;
        }

        LocalDateTime now = LocalDateTime.now();
        for (CourseClassAssociation association : courseClassAssociations) {
            // 设置创建和更新时间
            association.setCreatedTime(now);
            association.setUpdatedTime(now);
        }

        return courseClassAssociationMapper.batchInsert(courseClassAssociations) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(CourseClassAssociation courseClassAssociation) {
        courseClassAssociationMapper.deleteByMainCourseId(courseClassAssociation.getMainCourseId());
        // 更新时间设为当前时间
        courseClassAssociation.setUpdatedTime(LocalDateTime.now());
        for (Long deputyCourseId : courseClassAssociation.getDeputyCourseIds()) {
            CourseClassAssociation courseClassAssociation1 = new CourseClassAssociation();
            courseClassAssociation1.setMainCourseId(courseClassAssociation.getMainCourseId());
            courseClassAssociation1.setDeputyCourseId(deputyCourseId);
            courseClassAssociationMapper.insert(courseClassAssociation1);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Long id) {
        if (id == null) {
            return false;
        }
        return courseClassAssociationMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemove(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return courseClassAssociationMapper.batchDelete(ids) > 0;
    }

    @Override
    public List<CourseClassAssociation> getByMainCourseId(Long mainCourseId) {
        if (mainCourseId == null) {
            return null;
        }
        return courseClassAssociationMapper.selectByMainCourseId(mainCourseId);
    }

    @Override
    public List<CourseClassAssociation> getByDeputyCourseId(Long deputyCourseId) {
        if (deputyCourseId == null) {
            return null;
        }
        return courseClassAssociationMapper.selectByDeputyCourseId(deputyCourseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByMainCourseId(Long mainCourseId) {
        if (mainCourseId == null) {
            return false;
        }
        return courseClassAssociationMapper.deleteByMainCourseId(mainCourseId) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByDeputyCourseId(Long deputyCourseId) {
        if (deputyCourseId == null) {
            return false;
        }
        return courseClassAssociationMapper.deleteByDeputyCourseId(deputyCourseId) > 0;
    }

    @Override
    public CourseClassAssociation getByMainAndDeputyCourseId(Long mainCourseId, Long deputyCourseId) {
        if (mainCourseId == null || deputyCourseId == null) {
            return null;
        }
        return courseClassAssociationMapper.selectByMainAndDeputyCourseId(mainCourseId, deputyCourseId);
    }
}
